package org.zasadnyy.redlions.players.client.ui;

import org.zasadnyy.redlions.players.shared.model.TournamentItem;
import org.zasadnyy.redlions.players.shared.remote.PlayersCrudRemote;
import org.zasadnyy.redlions.players.shared.remote.PlayersCrudRemoteAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.smartgwt.client.util.SC;

public class TournamentEditPanel extends Composite implements
		HasValueChangeHandlers<TournamentItem> {

	private static TournamentEditPanelUiBinder uiBinder = GWT
			.create(TournamentEditPanelUiBinder.class);

	interface TournamentEditPanelUiBinder extends
			UiBinder<Widget, TournamentEditPanel> {
	}

	@UiField
	Button createButton;
	@UiField
	Button deleteButton;
	@UiField
	Button saveButton;
	@UiField
	TextBox titleField;
	@UiField
	TextBox urlField;
	@UiField
	DateBox timeField;

	private TournamentItem tournament = new TournamentItem();
	private PlayersCrudRemoteAsync servise = GWT
			.create(PlayersCrudRemote.class);

	public TournamentEditPanel() {
		initWidget(uiBinder.createAndBindUi(this));

		DateTimeFormat birthdayDateFormat = DateTimeFormat
				.getFormat(PredefinedFormat.DATE_LONG);
		timeField.setFormat(new DateBox.DefaultFormat(birthdayDateFormat));
	}

	public void setTournament(TournamentItem tournament) {
		if (tournament != null) {
			this.tournament = tournament;

			titleField.setText(tournament.getTitle());
			timeField.setValue(tournament.getTime());
			urlField.setText(tournament.getUrl());
		} else {
			this.tournament = new TournamentItem();

			titleField.setText("");
			timeField.setValue(null);
			urlField.setText("");
		}
	}

	public TournamentItem getTournament() {
		return syncTournamentFields();
	}

	private TournamentItem syncTournamentFields() {
		tournament.setTitle(titleField.getText());
		tournament.setUrl(urlField.getText());
		tournament.setTime(timeField.getValue());
		return tournament;
	}

	@UiHandler("createButton")
	void onCreateButtonClick(ClickEvent event) {
		setTournament(null);
		// ValueChangeEvent.fire(this, null);
	}

	@UiHandler("saveButton")
	void onSaveButtonClick(ClickEvent event) {
		final TournamentItem changedTournament = syncTournamentFields();
		
		if (changedTournament == null) {
			return;
		}
		
		servise.addOrEditTournment(changedTournament,
				new AsyncCallback<Void>() {

					@Override
					public void onSuccess(Void result) {
						ValueChangeEvent.fire(TournamentEditPanel.this,
								changedTournament);
					}

					@Override
					public void onFailure(Throwable caught) {
						SC.warn("Thing are bad. Exeption: "
								+ caught.getMessage());
					}
				});
	}

	@UiHandler("deleteButton")
	void onDeleteButtonClick(ClickEvent event) {
		servise.deleteTournamentById(getTournament().getId(),
				new AsyncCallback<Void>() {
	
					@Override
					public void onSuccess(Void result) {
						ValueChangeEvent.fire(TournamentEditPanel.this, null);
						setTournament(null);
					}
	
					@Override
					public void onFailure(Throwable caught) {
						SC.warn("Thing are bad. Exeption: "
								+ caught.getMessage());
					}
				});
	
	}

	@Override
	public HandlerRegistration addValueChangeHandler(
			ValueChangeHandler<TournamentItem> handler) {
		return addHandler(handler, ValueChangeEvent.getType());
	}
}
